package com.xiguaji.tiktok.v1.ins.rank.repository.impl;

import com.xiguaji.boot.toolkit.jdbc.JdbcTemplateHelper;
import com.xiguaji.boot.toolkit.jdbc.Page;
import com.xiguaji.tiktok.v1.ins.rank.dto.BrandRankDto;
import com.xiguaji.tiktok.v1.ins.rank.dto.InsRkSearch;
import com.xiguaji.tiktok.v1.ins.rank.repository.InsRkBrandDayExtRepository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class InsRkBrandDayExtRepositoryImpl implements InsRkBrandDayExtRepository {

    @Override
    public Page<BrandRankDto> findPage(InsRkSearch rkSearch) {
        String select = "select b.id,b.en_name,b.logo,b.category_name,r.media_count,r.like_count,r.review_count,r.tec engagements,r.rank";
        String from = " from ins_rk_brand_day r left join common_brand b on r.bid = b.id where datecode = ? ";
        List<Object> params = new ArrayList<>();
        params.add(rkSearch.getDate());

        if(StringUtils.isEmpty(rkSearch.getCategoryName())){
            params.add("");
        }else{
            params.add(rkSearch.getCategoryName());
        }
        from += " and r.category_name = ? ";
        from+= " order by r.rank ";
        return JdbcTemplateHelper.paginate(BrandRankDto.class,rkSearch.getPage(),rkSearch.getPageSize(),select,from,params.toArray());
    }

    @Override
    public List<BrandRankDto> findList(InsRkSearch rkSearch, int size) {
        String sql = "select b.id,b.en_name,b.logo,b.category_name,r.media_count,r.like_count,r.review_count,r.tec engagements,r.rank" +
                " from ins_rk_brand_day r left join common_brand b on r.bid = b.id where datecode = ? ";
        List<Object> params = new ArrayList<>();
        params.add(rkSearch.getDate());

        if(StringUtils.isEmpty(rkSearch.getCategoryName())){
            params.add("");
        }else{
            params.add(rkSearch.getCategoryName());
        }
        sql += " and r.category_name = ? ";
        sql+= " order by r.rank limit 0,?";
        params.add(size);
        return JdbcTemplateHelper.findEntity(BrandRankDto.class,sql,params.toArray());
    }
}
